package com.yycx.app.provider.service.impl;

import com.yycx.common.base.service.CommonAppService;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.security.MyClientDetails;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationException;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.stereotype.Service;

/**
 * @author: zyf
 * @date: 2018/11/12 16:26
 * @description:
 */
@Slf4j
@Service
public class AppClientDetailsService implements ClientDetailsService {


    @Autowired(required = false)
    private CommonAppService commonAppService;


    @Override
    public ClientDetails loadClientByClientId(String clientId) throws ClientRegistrationException {
        MyClientDetails details = commonAppService.getAppClientInfo(clientId);
        if (details == null) {
            throw new NoSuchClientException("无效的客户端: " + clientId);
        }
        Integer status = 1;
        if (!CommonConstants.ENABLED.equals(status)) {
            throw new ClientRegistrationException("客户端已被禁用");
        }
        return details;
    }
}
